﻿using Framework;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace PokerGame
{
    public class MatchPanel : UIBase
    {
        private Button btnMatch;
        private Image imgBg;
        private Text txtDes;
        private Button btnCancel;
        private Button btnEnter;

        #region runtime
        private void Awake()
        {
            Bind(UIEvent.SHOW_ENTER_ROOM_BTN);
            btnMatch = transform.Find("btnMatch").GetComponent<Button>();
            imgBg = transform.Find("imgBg").GetComponent<Image>();
            txtDes = transform.Find("txtDes").GetComponent<Text>();
            btnCancel = transform.Find("btnCancel").GetComponent<Button>();
            btnEnter = transform.Find("btnEnter").GetComponent<Button>();
        }

        private void Start()
        {
            SetMatchUIActive(false);
            btnEnter.gameObject.SetActive(false);
            btnMatch.onClick.AddListener(BtnMatch_OnClick);
            btnCancel.onClick.AddListener(BtnCancel_OnClick);
        }
        private void Update()
        {
            //更新匹配字段
            if (txtDes.gameObject.activeInHierarchy)
            {
                timer += Time.deltaTime;
                if (timer >= intervalTime)
                {
                    timer = 0;
                    MatchAnimation();
                }
            }
        }
        private void OnDestroy()
        {
            btnMatch.onClick.RemoveListener(BtnMatch_OnClick);
            btnCancel.onClick.RemoveListener(BtnCancel_OnClick);
        }
        #endregion

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.SHOW_ENTER_ROOM_BTN:
                    btnEnter.gameObject.SetActive(true);
                    break;
                default:
                    break;
            }
        }

        #region private method
        private void BtnMatch_OnClick()
        {
            //TODO: 向客户端发起开始匹配请求
            SetMatchUIActive(true);
        }

        private void BtnCancel_OnClick()
        {
            //TODO: 向客户端发起取消匹配请求
            SetMatchUIActive(false);
        }

        /// <summary>
        /// 匹配动画是否显示
        /// </summary>
        private void SetMatchUIActive(bool active)
        {
            imgBg.gameObject.SetActive(active);
            txtDes.gameObject.SetActive(active);
            btnCancel.gameObject.SetActive(active);
            ReStartMatchAnimation();
        }
        #endregion

        #region match ui animation
        private static readonly string defaultText = "正在寻找房间";
        private static StringBuilder stringBuilder = new();
        private static int dotCount = 0;
        private static readonly float intervalTime = 0.25f;
        private static float timer = 0;

        private static void ReStartMatchAnimation()
        {
            dotCount = 0;
            stringBuilder.Clear();
            stringBuilder.Append(defaultText);
        }
        private void MatchAnimation()
        {
            if (dotCount++ < 4)
            {
                stringBuilder.Append('.');
            }
            else
            {
                ReStartMatchAnimation();
            }
            txtDes.text = stringBuilder.ToString();
        }
        #endregion
    }
}
